package com.qianfeng.xinyihezi.xinyihezi.activity.activity_meng;

import android.app.ProgressDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.ImageView;
import android.widget.ListView;

import com.qianfeng.xinyihezi.xinyihezi.R;
import com.qianfeng.xinyihezi.xinyihezi.adapter.adapter_meng.MyUserAdapter;
import com.qianfeng.xinyihezi.xinyihezi.model.bean_meng.User;
import com.qianfeng.xinyihezi.xinyihezi.netinterface.RetrofitInterface;
import com.qianfeng.xinyihezi.xinyihezi.url.NetURL;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class UserActivity extends AppCompatActivity implements View.OnClickListener {
    private Retrofit mRetrofit;
    private RetrofitInterface mInterface;
    private ListView mListView;
    private List<User> mList;
    private MyUserAdapter mAdapter;
    private ProgressDialog mDialog;
    private ImageView mImageView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_user);
        initView();
        initData();
        setData();
        setListener();
    }

    private void initView() {
        mListView = (ListView) findViewById(R.id.lv_user);
        mImageView = (ImageView) findViewById(R.id.iv_user_back);
    }

    private void initData() {
        mDialog = new ProgressDialog(this);
        mDialog.setMessage("加载中...");
        mDialog.show();
        mRetrofit = new Retrofit.Builder()
                // 设置基地址
                .baseUrl(NetURL.HEAD_URL)
                // 设置转换工厂  用于Json解析
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        // 初始化接口实例
        mInterface = mRetrofit.create(RetrofitInterface.class);
        mList = new ArrayList<>();
        loadUser();
    }

    private void loadUser() {
        Log.e("111111111111111", "" + Integer.parseInt(getIntent().getStringExtra("stra_id")));
        mInterface.getNetUser(Integer.parseInt(getIntent().getStringExtra("stra_id"))).enqueue(
                new Callback<ResponseBody>() {
                    @Override
                    public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                        if (response.isSuccessful()) {
                            try {
                                String result = response.body().string().toString();
                                Log.e("111111111111111", result);
                                JSONObject object = new JSONObject(result);
                                JSONObject data = object.optJSONObject("data");
                                JSONArray array = data.optJSONArray("avatar");
                                for (int i = 0; i < array.length(); i++) {
                                    User user = new User();
                                    JSONObject object1 = array.optJSONObject(i);
                                    user.avatar = object1.optString("avatar");
                                    user.name = object1.optString("name");
                                    user.time = object1.optString("time");
                                    mList.add(user);
                                }
                                mAdapter.notifyDataSetChanged();
                            } catch (Exception e) {
                            } finally {
                                mDialog.dismiss();
                            }
                        }
                    }

                    @Override
                    public void onFailure(Call<ResponseBody> call, Throwable t) {
                    }
                }

        );
    }

    private void setData() {
        mAdapter = new MyUserAdapter(mList, this);
        mListView.setAdapter(mAdapter);
    }

    private void setListener() {
        mImageView.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        finish();
    }
}
